package com.qingge.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qingge.springboot.common.Result;
import com.qingge.springboot.entity.Sc;
import com.qingge.springboot.service.IScService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

    @RestController
    @RequestMapping("/sc")
    public class ScController {

        @Resource
        private IScService scService;

        // 新增或者更新
        @PostMapping
        public Result save(@RequestBody Sc sc) {
            scService.saveOrUpdate(sc);
            return Result.success();
        }

        @DeleteMapping("/{id}")
        public Result delete(@PathVariable Integer id) {
            scService.removeById(id);
            return Result.success();
        }

        @PostMapping("/del/batch")
        public Result deleteBatch(@RequestBody List<Integer> ids) {
            scService.removeByIds(ids);
            return Result.success();
        }

        @GetMapping
        public Result findAll() {
            return Result.success(scService.list());
        }

        @GetMapping("/{id}")
        public Result findOne(@PathVariable Integer id) {
            return Result.success(scService.getById(id));
        }

        @GetMapping("/page")
        public Result findPage(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam String name,
                               @RequestParam Integer uid) {
            QueryWrapper<Sc> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("uid",uid);
            queryWrapper.like("name",name);
            queryWrapper.orderByDesc("id");
            return Result.success(scService.page(new Page<>(pageNum, pageSize), queryWrapper));
        }

    }
